package com.carbon.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.core.domain.AjaxResult;
import com.carbon.common.utils.SecurityUtils;
import com.carbon.domain.Batch;
import com.carbon.domain.Product;
import com.carbon.domain.Provider;
import com.carbon.service.BatchService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import com.carbon.service.IProductService;
import com.carbon.vo.SearchParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 生产批号(Batch)表控制层
 *
 * @author WangFK
 * @since 2023-08-22 20:28:46
 */
@RestController
@Slf4j
@RequestMapping("/wms/batch")
public class BatchController {
    /**
     * 服务对象
     */
    @Resource
    private BatchService batchService;
    @Resource
    private  IProductService productService;

    /**
     * 修改生产批号状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/changeBatchStatus")
    public AjaxResult changeProviderStatus(Integer id,String status){
        Batch batch = new Batch();
        batch.setId(id);
        batch.setStatus(status);
        return batchService.updateById(batch)?AjaxResult.success():AjaxResult.error();
    }
    /**
     * 分页查询
     * @param params
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(SearchParams params){
        Page<Batch> batchPage = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaUpdateWrapper<Batch> lqw = new LambdaUpdateWrapper<Batch>()
                .like(ObjectUtil.isNotEmpty(params.getSearch()), Batch::getBatchId, params.getSearch())
                .eq(ObjectUtil.isNotEmpty(params.getProductId()), Batch::getProductId, params.getProductId())
                .orderByAsc(Batch::getSort)
                .orderByAsc(Batch::getCreateTime);
        batchPage = batchService.page(batchPage, lqw);
        List<Batch> records = batchPage.getRecords();
        if (ObjectUtil.isEmpty(records)){
            return AjaxResult.success()
                    .put("total",batchPage.getTotal());
        }
        records.stream().forEach(item->{
            Product byId = productService.getById(item.getProductId());
            if (ObjectUtil.isNotEmpty(byId)){
                item.setProductName(byId.getProductName());
                item.setProductImg(byId.getImg());
            }
        });
        return AjaxResult.success()
                .put("total",batchPage.getTotal())
                .put("rows",records);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        Batch byId = batchService.getById(id);
        Product byId1 = productService.getById(byId.getProductId());
        byId.setProductName(byId1.getProductName());
        byId.setProductImg(byId1.getImg());
        return AjaxResult.success(byId);
    }

    /**
     * 新增数据
     *
     * @param batch 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Batch batch) {
        String batchId = batch.getBatchId();
        if (ObjectUtil.isEmpty(batch.getProductId())){
            return AjaxResult.error("请选择生产单位");
        }
        if (ObjectUtil.isEmpty(batchId)){
            batchId = DateUtil.now();
        }
        batchId=StrUtil.addSuffixIfNot(StrUtil.addPrefixIfNot(batchId, "【"),"】");
        System.out.println(batchId);
        if (ObjectUtil.isNotEmpty(batch.getId())){
            return update(batch);
        }
        batch.setBatchId(batchId);
        LambdaUpdateWrapper<Batch> lqw = new LambdaUpdateWrapper<Batch>()
                .eq(Batch::getBatchId,batchId);
        if (ObjectUtil.isNotEmpty(batchService.list(lqw))){
            return AjaxResult.error("生产批号:"+batchId+"重复");
        }

        return batchService.save(batch)?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 修改数据
     *
     * @param batch 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Batch batch) {
        Batch byId = batchService.getById(batch.getId());
        String batchId = batch.getBatchId();
        if (!batchId.equalsIgnoreCase(byId.getBatchId())){
            LambdaUpdateWrapper<Batch> lqw = new LambdaUpdateWrapper<Batch>()
                    .eq(Batch::getBatchId, batchId);
            if (ObjectUtil.isNotEmpty(batchService.list(lqw))){
                return AjaxResult.error("生产批号:"+ batchId +"重复");
            }
            batchId=StrUtil.addSuffixIfNot(StrUtil.addPrefixIfNot(batchId, "【"),"】");
            batch.setBatchId(batchId);
        }
        return batchService.updateById(batch)?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable String id) {
        List<String> split = StrUtil.split(id, ",");
        LambdaUpdateWrapper<Batch> lqw = new LambdaUpdateWrapper<Batch>()
                .in(Batch::getId, split);
        return batchService.remove(lqw)?AjaxResult.success():AjaxResult.error();
    }
}

